package com.lesso.lzh.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lzh.sunflower.domain.repository.EmployeeAttendanceRuleRepository;
import com.lesso.lzh.sunflower.infra.assembler.EmployeeAttendanceRuleAssembler;
import com.lesso.lzh.sunflower.infra.mapper.EmployeeAttendanceRuleMapper;
import com.lesso.lzh.sunflower.model.EmployeeAttendanceRule;
import com.lesso.lzh.sunflower.pojo.dto.EmployeeAttendanceRuleQueryDTO;
import com.lesso.lzh.sunflower.pojo.vo.EmployeeAttendanceRuleVO;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * EmployeeAttendanceRuleRepositoryImpl
 *
 * @author zqz
 * @since 2025-07-04
 */
@Repository
public class EmployeeAttendanceRuleRepositoryImpl implements EmployeeAttendanceRuleRepository {
    private final EmployeeAttendanceRuleMapper employeeAttendanceRuleMapper;

    public EmployeeAttendanceRuleRepositoryImpl(EmployeeAttendanceRuleMapper employeeAttendanceRuleMapper) {
        this.employeeAttendanceRuleMapper = employeeAttendanceRuleMapper;
    }

    @Override
    public Long save(EmployeeAttendanceRule rule) {
        rule.setCreateTime(LocalDateTime.now());
        rule.setUpdateTime(LocalDateTime.now());
        rule.setDelStatus(0); // 初始为正常状态
        employeeAttendanceRuleMapper.insert(rule);
        return rule.getId();
    }

    @Override
    public Page<EmployeeAttendanceRuleVO> getPageList(EmployeeAttendanceRuleQueryDTO dto) {
        Page<EmployeeAttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<EmployeeAttendanceRule> wrapper = new EntityWrapper<>();
        // 动态拼接查询条件
        wrapper.eq(dto.getEmployeeId() != null, "employee_id", dto.getEmployeeId())
                .eq(StringUtils.hasText(dto.getRuleCode()), "rule_code", dto.getRuleCode())
                .eq(dto.getEffectiveDate() != null, "effective_date", dto.getEffectiveDate())
                .eq(dto.getExpiryDate() != null, "expiry_date", dto.getExpiryDate())
                .eq("del_status", 0); // 过滤已删除数据
        // 查询并转换为VO
        List<EmployeeAttendanceRule> rules = employeeAttendanceRuleMapper.selectPage(page, wrapper);
        return page.setRecords(EmployeeAttendanceRuleAssembler.INSTANCE.entityListToVOList(rules));
    }

    @Override
    public Page<EmployeeAttendanceRuleVO> getCustomPageList(EmployeeAttendanceRuleQueryDTO dto) {
        Page<EmployeeAttendanceRuleVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<EmployeeAttendanceRuleVO> list = employeeAttendanceRuleMapper.selectPageList(page, dto);
        return page.setRecords(list);
    }

    @Override
    public void removeById(Long id) {
        employeeAttendanceRuleMapper.deleteById(id);
    }

    @Override
    public EmployeeAttendanceRule selectById(Long id) {
        return employeeAttendanceRuleMapper.selectById(id);
    }

    @Override
    public void update(EmployeeAttendanceRule rule) {
        rule.setUpdateTime(LocalDateTime.now()); // 更新时刷新时间
        employeeAttendanceRuleMapper.updateSelective(rule);
    }
}